const app = getApp()
import BaseModel from '../../utils/common.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navId:"1",//导航id
    page:1,
    size:6, //每页条数
    is_clickTab:0,//1是上拉，0是首次
    newsAllData:"", //资讯数组
    footer_more:"",
    empty_title:"",
    empty_img:"",
    empty_again:"",//重试
    },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.navId==1){
      this.getPicAllData();
    }else if(this.data.navId==2){
      this.getNewsAllData();
    }else if(this.data.navId==3){
      this.getVideoListData();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /*图片列表*/
  getPicAllDataModel: new BaseModel({
    url: 'employees/listMaterialImg',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getPicAllData() {
    this.getPicAllDataModel
        .fetch({
          "page": this.data.page,
          "size": this.data.size
        })
        .then(res => {
          if(this.data.is_clickTab==0){ //首次
            this.setData({
              newsAllData: res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              //成功
              var newsAllData_list = this.data.newsAllData;
              for(var i=0;i<res.data.length;i++){
                newsAllData_list.push(res.data[i])
              }
              this.setData({
                newsAllData: newsAllData_list
              })
            }
          }
        })
        .catch(res => {
          //失败
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
  /*文章列表*/
  getNewsAllDataModel: new BaseModel({
    url: 'employees/listNews',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getNewsAllData() {
    this.getNewsAllDataModel
        .fetch({
          "page": this.data.page,
          "size": this.data.size
        })
        .then(res => {
          if(this.data.is_clickTab==0){ //首次
            this.setData({
              newsAllData: res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              //成功
              var newsAllData_list = this.data.newsAllData;
              for(var i=0;i<res.data.length;i++){
                newsAllData_list.push(res.data[i])
              }
              this.setData({
                newsAllData: newsAllData_list
              })
            }
          }
        })
        .catch(res => {
          //失败
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
  /*视频列表*/
  getVideoListModel: new BaseModel({
    url: 'employees/listVideo',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getVideoListData() {
    var that =this;
    that.getVideoListModel
        .fetch({
          "page": that.data.page,
          "size": that.data.size
        })
        .then(res => {
          if(this.data.is_clickTab==0){ //首次
            this.setData({
              newsAllData: res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              //成功
              var newsAllData_list = this.data.newsAllData;
              for(var i=0;i<res.data.length;i++){
                newsAllData_list.push(res.data[i])
              }
              this.setData({
                newsAllData: newsAllData_list
              })
            }
          }
        })
        .catch(res => {
          //失败
          that.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },

  /*导航切换*/
  clickNav:function(e){
    this.setData({
      navId:e.currentTarget.dataset.id
    })
    this.setData({
      is_clickTab:0,
      page:1
    })
    if(this.data.navId==1){
      this.getPicAllData();
    }else if(this.data.navId==2){
      this.getNewsAllData();
    }else if(this.data.navId==3){
      this.getVideoListData();
    }
  },
  /*一键保存图片*/
  download:function(e){
    var index;
    var that = this;
    var picArr=e.currentTarget.dataset.img;
    wx.getSetting({
      success(res) {
        /*已经授权*/
        if(res.authSetting['scope.writePhotosAlbum']==true){
          that.xiazaiTupian(picArr,index)
          console.log("已经授权")
        }else{
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              // 用户已经同意小程序使用保存到相册功能，后续调用 wx.startRecord 接口不会弹窗询问
              that.xiazaiTupian(picArr,index)
            },
            fail(){
              wx.showModal({
                title: '温馨提示',
                content: '你关闭了访问相册的权限，无法保存，请允许访问相册',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success(settingdata) {
                        if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                          that.xiazaiTupian(picArr,index)
                        } else {
                          console.log("获取权限失败")
                        }
                      }
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          })
          /*wx.openSetting({
            success(settingdata) {
              console.log(settingdata)
              if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                console.log("获取权限成功，再次点击图片保存到相册")
                that.xiazaiTupian(picArr,index)
              } else {
                console.log("获取权限失败")
              }
            }
          })*/
        }
      }
    })
  },
  //下载内容
  xiazaiTupian: function (picArr,index) {
    let that=this;
    index=index||0;
    wx.downloadFile({
      url: picArr[index],
      success: function (res) {
        var temp = res.tempFilePath
        wx.saveImageToPhotosAlbum({
          filePath: temp,
          success: function () {
            index+=1;
            if (index < picArr.length){
              that.xiazaiTupian(picArr, index);
            }else{
              wx.showToast({
                title: '下载完成',
              })
            }
          },
          fail: function () {
            index += 1;
            if (index < picArr.length) {
              that.xiazaiTupian(picArr, index);
            } else {
              wx.showToast({
                title: '下载完成',
              })
            }
            wx.showToast({
              title: '第' + (index+1) + '下载失败',
            })
          }
        })
      },
      fail: function (res) {
        console.log("下载失败")
        wx.showToast({
          title: '下载失败',
        })
      },
      complete(res){
        console.log("完成")
      }
  })

},
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var imgArr = e.currentTarget.dataset.imagelist;
    var currentImg = imgArr[e.target.dataset.img];
    wx.previewImage({
      current: currentImg, // 当前显示图片的http链接
      urls: imgArr
    })
  },
  /*复制文案*/
  copy:function(e){
    console.log(e)
    var this_message = e.currentTarget.dataset.text;
    wx.setClipboardData({
      data: this_message,
      success(res) {
        wx.getClipboardData({
          success(res) {
          }
        })
      }
    })
  },
  //重试
  again_request:function(){
    this.setData({
      is_clickTab:0,
      page:1
    })
    if(this.data.navId==1){
      this.getPicAllData();
    }else if(this.data.navId==2){
      this.getNewsAllData();
    }else if(this.data.navId==3){
      this.getVideoListData();
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    if(this.data.navId==1){
      this.getPicAllData();
    }else if(this.data.navId==2){
      this.getNewsAllData();
    }else if(this.data.navId==3){
      this.getVideoListData();
    }
    wx.stopPullDownRefresh(); //停止下拉
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    this.setData({
      is_clickTab:1,//1是上拉，追加数据
    })
    if(this.data.navId==1){
      this.getPicAllData();
    }else if(this.data.navId==2){
      this.getNewsAllData();
    }else if(this.data.navId==3){
      this.getVideoListData();
    }
  }
})